#include <stdlib.h>
#include <assert.h>
#include "ThreadPool.h"
#include "Log.h"


struct ThreadPool *threadPoolInit(struct EventLoop *mainLoop, int32_t count)
{   
    struct ThreadPool *pool = (struct ThreadPool*)malloc(sizeof(struct ThreadPool));
    pool->index = 0;
    pool->isStart = false;
    pool->mainLoop = mainLoop;
    pool->threadNum = count;
    pool->workerThreads = (struct WorkerThread*)malloc(sizeof(struct WorkerThread) * count);
    return pool;
}

void threadPollRun(struct ThreadPool *pool)
{
    assert(pool && !pool->isStart);
    if(pool->mainLoop->threadID != pthread_self())  //判断是否为主线程调用threadPollRun()
    {
        exit(0);
    }
    pool->isStart = true;
    if(pool->threadNum)
    {
        for(int32_t i = 0; i < pool->threadNum; ++i)
        {
            workerThreadInit(&pool->workerThreads[i], i);
            workerThreadRun(&pool->workerThreads[i]);
        }
    }
}

struct EventLoop *takeWorkerEventLoop(struct ThreadPool *pool)
{   
    assert(pool->isStart);
    if(!pthread_equal(pool->mainLoop->threadID, pthread_self()))//判断是否为主线程调用pthread_self()
    {
        exit(0);
    }
    //从线程池中找一个子线程，然后取出里面的反应堆实例
    struct EventLoop *evLoop = pool->mainLoop;
    if(pool->threadNum > 0)
    {
        evLoop = pool->workerThreads[pool->index].evLoop;
        pool->index++;
        pool->index = pool->index % pool->threadNum;
    }
    return evLoop;
}
